{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 寻找两个正序数组的中位数\n",
    "def findMediumSortedArrays(nums1, nums2):\n",
    "    n1,n2 = len(nums1),len(nums2)\n",
    "    if n1>n2:\n",
    "        return findMediumSortedArrays(nums2,nums1)\n",
    "    # n1<=n2\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        if n1 > n2:\n",
    "            return self.findMedianSortedArrays(nums2, nums1)\n",
    "        cnt = (n1 + n2 + 1) // 2\n",
    "        left, right = 0, n1\n",
    "        while left < right:\n",
    "            mid1 = (left + right) // 2\n",
    "            mid2 = cnt - mid1 - 1\n",
    "            if nums1[mid1] >= nums2[mid2]:\n",
    "                right = mid1\n",
    "            else:\n",
    "                left = mid1 + 1\n",
    "        cnt1, cnt2 = left, cnt - left\n",
    "        left_val = max(nums1[cnt1 - 1] if cnt1 else float('-inf'), nums2[cnt2 - 1] if cnt2 else float('-inf'))\n",
    "        if (n1 + n2) % 2 == 1:\n",
    "            return left_val\n",
    "        right_val = min(nums1[cnt1] if cnt1 < n1 else float('inf'), nums2[cnt2] if cnt2 < n2 else float('inf'))\n",
    "        return (left_val + right_val) / 2\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
